
package com.zkmonitor.service;

import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zkmonitor.biz.NodeBiz;
import com.zkmonitor.dao.NodeDAO;
import com.zkmonitor.exception.ShepherException;

import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;

@Service
public class NodesService {

    private static Logger logger = LoggerFactory.getLogger(NodesService.class);

    @Autowired
    private NodeDAO nodeDAO;

    @Autowired
    private NodeBiz nodeBiz;

    public List<String> getChildren(String cluster, String path) throws ShepherException {
        List<String> children = nodeDAO.getChildren(cluster, path);
        Collections.sort(children);
        return children;
    }

    public String getData(String cluster, String path) throws ShepherException {
        return nodeDAO.getData(cluster, path);
    }
    
    public Entry<List<ACL>,Stat> getStatWithACL(String cluster, String path) throws ShepherException {
        return nodeDAO.getStatWithAcl(cluster, path);
    }

    public Stat getStat(String cluster, String path) throws ShepherException {
        return this.getStat(cluster, path, true);
    }

    public Stat getStat(String cluster, String path, boolean returnNullIfPathNotExists) throws ShepherException {
        return nodeDAO.getStat(cluster, path, returnNullIfPathNotExists);
    }

    public void create(String cluster, String path, String data, String creator) throws ShepherException {
        nodeBiz.create(cluster, path, data);
        logger.info("Create node, cluster={}, path={}, operator={}", cluster, path, creator);
    }

    public void create(String cluster, String path, String data, String creator, boolean createParents) throws ShepherException {
        nodeBiz.create(cluster, path, data, createParents);
        logger.info("Create node, cluster={}, path={}, operator={}", cluster, path, creator);
    }

    public void createEphemeral(String cluster, String path, String data, String creator) throws ShepherException {
        nodeBiz.createEphemeral(cluster, path, data);
        logger.info("Create ephemeral node, cluster={}, path={}, operator={}", cluster, path, creator);
    }

   
    public void update(String cluster, String path, String data, String creator) throws ShepherException {
        nodeBiz.update(cluster, path, data);
        logger.info("Update node, cluster={}, path={}, operator={}", cluster, path, creator);
    }

    public void delete(String cluster, String path, String creator) throws ShepherException {
        nodeBiz.delete(cluster, path);
        logger.info("Delete node, cluster={}, path={}, operator={}", cluster, path, creator);
    }
}
